const initialState = {
  articles: {} as Articlestatetype,
};
export type ArticleActionType =
  | {
      type: 'article/saveList';
      payload: {
        results: ArticleType[];
        pre_timestamp: number;
        channel_id: number;
      };
    }
  | {
      type: 'article/udpatelistById';
      payload: {
        results: ArticleType[];
        pre_timestamp: number;
        channel_id: number;
      };
    };
export default function articleReducer(
  state = initialState,
  action: ArticleActionType
): typeof initialState {
  switch (action.type) {
    // 2.编写reducer分支
    case 'article/saveList':
      console.log(
        'action.payload.channel_id  ----->  ',
        action.payload.channel_id
      );
      const { channel_id, pre_timestamp, results } = action.payload;
      return {
        ...state,
        articles: {
          ...state.articles,
          [channel_id]: {
            pre_timestamp,
            results,
          },
        },
      };
    case 'article/udpatelistById':
      const {
        channel_id: update_id,
        pre_timestamp: update_timestamp,
        results: update_results,
      } = action.payload;
      const oldResultsById = state.articles[update_id]?.results || [];
      const newResultsById = [...oldResultsById, ...update_results];
      return {
        ...state,
        articles: {
          ...state.articles,
          [update_id]: {
            results: newResultsById,
            pre_timestamp: update_timestamp,
          },
        },
      };
    default:
      return state;
  }
}
export type ArticleType = {
  art_id: string;
  title: string;
  aut_id: string;
  comm_count: number;
  pubdate: string;
  aut_name: string;
  is_top: number;
  cover: {
    type: number;
    images: string[];
  };
};
type Articlestatetype = {
  [channel: number]: {
    results: ArticleType[];
    pre_timestamp: number;
  };
};
